<?php
/**
 * Akimimi front
 * ShowOrderView
 * by M.Wang 
 */

class ShowOrderView extends BaseView {

    public $order;
    public $items;
    public $isSuperAgent;
    public $userid;
    public $constants;
    public $aboutOrderStepUser = array(
        ABOUTORDERSTATUS1TXT,
        ABOUTORDERSTATUS3TXT,
        ABOUTORDERSTATUS4TXT,
        ABOUTORDERSTATUSNTXT,
        ABOUTORDERSTATUS5TXT
    );
    public $aboutOrderStepAgent = array(
        ABOUTORDERSTATUS11TXT,
        ABOUTORDERSTATUS12TXT,
        ABOUTORDERSTATUS14TXT,
        ABOUTORDERSTATUS13TXT,
        ABOUTORDERSTATUS15TXT
    );
    
    public function __construct(){
        parent::__construct();
        $this->constants = get_defined_constants();
    }

    public function getPage(){
        $arr_navsetting = array(
            array('url'=>'/userinfo/index', 'text'=>MYAKIMIMITXT),
            array('text'=>OLMYORDERTXT)
        );
        $this->setPageTitle(OLMYORDERTXT);
        $this->addCSS('order_detail.css');
        $this->addCSS('myaccountleftnav.css');
        $str_js = "
            <script type='text/javascript'>
            var orderActionNextStep = function(int_orderid,int_status){
                $('#oaf_selfModel').val(" . ($this->selfModel ? '1' : '') . ");
                $('#oaf_order_id').val(int_orderid);
                $('#oaf_order_status').val(int_status);
                $('#orderActionForm').submit();
            };
            function openDialog(){
        	    var int_width = window.screen.availWidth;
        	    var int_height = window.screen.availHeight;
        	    $( 'body' ).css('overflow-x','hidden');
        	    $('#paymentDialog').css('width',int_width + 'px');
        	    $('#paymentDialog').css('height',window.document.body.clientHeight + 'px');
        	    $('#paymentDialogContent').css('margin-top', (int_height > 120 ? (int_height-120)*0.3 : 0) + 'px');
        	    $('#paymentDialogContent').css('margin-left', (int_width > 320 ? (int_width-320)*0.5 : 0) + 'px');
        	    $('#paymentDialog').css('display','block');
        	    window.scroll(0,0);
            }
            </script>";
        $this->setHeadExtends($str_js);
        $header = '';
        require_once '../app/views/MyAccoutLeftNavView.php';
        $myAccountLeftNavView = new MyAccountLeftNav();
        $str_result = $header
                    . $this->getNavigation($arr_navsetting)
                    . "<div id='mymimi_body'>"
                    . $myAccountLeftNavView->getMyAccountLeftNav()
                    . $this->getContent()
                    . "</div>";
        return $this->getHtmlPage($str_result);
    }

    public function getContent(){
        $str_result = "<div id='mymimi_right'>"
               . "<div class='right_title'>" . $this->getTitle() . '</div>'
               . $this->getOrderNav()
               . "<div id='order_ts'>" . $this->getMessage(). '</div>'
               . "<div id='order_list'>" . $this->getItems($this->order['items']). '</div>'
               . "<div id='order_xx'>" . $this->getOrderDetail() . '</div>'
               . '</div>'
               . "<form id='orderActionForm' method='post' action='/userinfo/nextstep'>
                    <input type='hidden' id='oaf_selfModel' name='selfModel' />
                    <input type='hidden' id='oaf_order_id' name='order_id' />
                    <input type='hidden' id='oaf_order_status' name='order_status' />
                    </form>";
        $str_result.= <<<str_result
        <div id="paymentDialog" style="left:0px;top:0px;background-color:#000;display:none;position:absolute;z-index:9;text-align:center;font-size:12px;filter:alpha(opacity=80);-moz-opacity:0.8;" onclick='try{event.cancelBubble = true;}catch(e){event.stopPropagation();}'>
            <div id='paymentDialogContent' style="position:static;height:120px;width:320px;background-color:#fff;border:1px solid #000;font-size:16px;">
                <div style='background-color:#690;line-height:24px;height:24px;;font-size:1em;text-align:left;'>
                    <span style='margin-left:5px;font-weight:700;color:#fff;'>操作提示</span>
                </div>
                <div style="margin-top:10px;height:48px;font-size:12px;line-height:24px;">
                    <span>请您在新打开的页面上完成操作</span><br/>
                    <span>完成前请<b>不要关闭</b>此窗口</span>
                </div>
                <div style='height:48px;padding:5px;'>
                    <input style="cursor:pointer" type="button" value="已完成"  onclick="window.location.reload();"/>
                    <input style="cursor:pointer" type="button" value="我遇到问题了" onclick="window.location='/u/help?type=buyprocess';"/>
                </div>
            </div>
        </div>
str_result;
        return $str_result;
    }

    protected function getOrderDetail(){
        $orderService = new OrderService();
        $str_result = '';
        $str_result.= "<div class='order_list_t'>" . ODORDERDETAILTXT . '<span>'
                    . OLORDERCUSTOMERTXT . $this->order['username'] . '</span></div>';
        $str_result.= "<div class='order_body' style='padding: 5px 0pt;'>";
        $str_result.= "<p class='style_1'>" . OLORDERCREATEDTIMETXT . $this->order['created'] . '</p>';
        $str_result.= "<p class='style_2'>" . ODORDERMODIFIEDTXT . $this->order['modified'] . '</p>';
        $str_result.= "<p class='style_3'>" . OLORDERSTATUSTXT . ($this->order['is_returned']=='1'?ORDERSSTATUSISRETURN:Order::getOrderStatusFront($this->order['status'])) . '</p>';
        if (!empty($this->order['agent_id'])) {
            $str_result.= "<p class='style_1'>" . ODCOLLEGETXT . $this->order['college_name'] . '</p>';
            $str_result.= "<p class='style_2'>" . ODAGENTNAMETXT . $this->order['agent_name'] . '</p>';
        }
        $str_result.= '</div>';

        $str_result.= "<div class='order_list_t'>" . ODPAYINFOTXT . '</div>';
        $str_result.= "<div class='order_body' style='padding: 5px 0pt;'>";
        $str_result.= "<p class='style_1'>" . OLORDERCOSTTYPETXT . Order::getOrderPaymentMethod( $this->order['payment_method'] ) . '</p>';
        $str_result.= "<p class='style_2'>" . ODPAYMENTSTATUSTXT . ( ( intval( $this->order['status'], 10 ) % 10 < 3 ) ? ODNOTPAY : (( intval( $this->order['status'], 10 ) % 10 > 5 ) ? ODOUTPAY : ODALREADYPAY ) ) . '</p>';
        $str_result.= "<p class='style_1'>" . ODPRODUCTTOTALTXT . MONEYTAG . $this->order['merchandise_total'] . '</p>';
        $str_result.= "<p class='style_2'>" . ODPRODUCTSHIPTXT . MONEYTAG . $this->order['shipping_fee'] . '</p>';
        $str_result.= "<p class='style_1'>" . ODOTHERCOSTTXT . MONEYTAG . $this->order['tax_fee'] . '</p>';
        $str_result.= "<p class='style_2'><span>" . ODALLTOTALTXT . MONEYTAG . $this->order['total'] . '</span></p>';
        $str_result.= '</div>';

        $str_result.= "<div class='order_list_t'>" . ODRECIVEINFOTXT . '</div>';
        $str_result.= "<div class='order_body' style='padding: 5px 0pt;'>";
        $str_result.= "<p class='style_1'>" . ODRECIVERTXT . $this->order['username'] . '</p>';
        $str_result.= "<p class='style_2'>" . ODRECIVEREMAILTXT . $this->order['delivery_email'] . '</p>';
        $str_result.= "<p class='style_1'>" . ODRECIVERMOBILETXT . $this->order['delivery_phone2'] . '</p>';
        $str_result.= "<p class='style_2'>" . ODRECIVERTELTXT . $this->order['delivery_phone1'] . '</p>';
        $str_result.= "<p class='style_3'>" . ODRECIVEAREATXT . $this->order['address1'] . $this->order['address2'] . $this->order['address3'] . '</p>';
        $str_result.= "<p class='style_3'>" . ODRECIVEADDRESSTXT . $this->order['delivery_detail_address'] . '</p>';
        $str_result.= "</div>";

        $str_result.= "<div class='order_list_t'>" . ODRECIVEINFOEXTENTXT . '</div>';
        $str_result.= "<div class='order_body' style='padding: 5px 0pt;'>";
        $str_result.= "<p class='style_1'>" . ODPOSTTIMETXT . Order::getShippingExpect($this->order['shipping_time']) . '</p>';
        $str_result.= "</div>";

        $str_result.= "<div class='order_list_t'>" . ODCHEQUEINFOTXT . '</div>';
        $str_result.= "<div class='order_body' style='padding: 5px 0pt;'>";
        $str_result.= "<p class='style_3'>" . ODCHEQUEHEADTXT . $this->order['invoice'] . '</p>';
        $str_result.= "<p class='style_3'>" . ODOTHERMEMOTXT . $this->order['remark'] . '</p>';
        $str_result.= "</div>";

        $str_result.= "<div style='text-align:right;margin-left:auto;margin-right:10px;margin-top:10px;font-size:1.2em;'>" . ($this->beableToAction($this->userid, $this->order)?$orderService->getOrderAction( $this->order['status'], $this->order['id'], $this->order['payment_method'] ):'') . '</div>';

        return $str_result;
    }

    protected function getItems(){
        $arr_items = $this->items;
        $str_result = "<div class='order_t'>" . ODPRODUCTLISTTXT . '</div>'
                    . "<div class='order_body'>"
                    . $this->getOrderListTableHead()
                    . $this->getOrderListItems($arr_items)
                    . $this->getOrderListTableFooter()
                    . '</div>';
        return $str_result;
    }

    protected function getOrderListTableFooter(){
        return "<div class='order_row_4'><ul>"
               . "<li class='style_2'>" . OLORDERITEMTOTALTXT . MONEYTAG . $this->order['total'] . '</li>'
               . "<li class='style_1'>" . OLORDERITEMFREIGHTTXT . MONEYTAG . $this->order['shipping_fee'] . '</li>'
               . '</ul></div>';
    }

    protected function getOrderListItems() {
        $arr_items = $this->items;
    	$str_content = '';
    	
    	
    	foreach ($arr_items as $arr_item) {
    		
    		$str_attribute = "<div>";
    		if(!empty($arr_item['attributes'])){
    	        foreach ($arr_item['attributes'] as $arr_attribute){
    	           $str_attribute .= $arr_attribute['attr'] . DQUOTECNTXT . '&nbsp;<span><font style="color:#0066ff;" >[' . $arr_attribute['attr_title'] . ']</font></span>&nbsp;&nbsp;'; 	
    	        }
    		}
    		$str_attribute .="</div>";
    		$item_name = empty($arr_item['title']) ? $arr_item['item_name'] : "<a href='/p/".urlencode($arr_item['url_title'])."'>{$arr_item['title']}</a>";
    		
    		$str_content .= <<< str_content
    		    <tr>
    		        <td style='border-bottom: dashed 1px  #DBDBDB; font-size:12px;'>
    		            <div style='margin: 5px 0 5px 8px;'>{$item_name}</div> 
str_content;
            if (!empty($arr_item['attributes'])){
            	$str_content .= "<div style='margin-bottom:5px; margin-left:8px;'>{$str_attribute}</div>";
           }
           if (!empty($arr_item['package_id'])){
           	    $str_content .= "<div style='margin-bottom:5px; margin-left:8px;' ><span>".ODSSHIPCOMNAMETXT ."<font style='color:#ff0000;' > ". AgentAccount::getShippingMethod( $arr_item['shipping_method_id'] ) . "</font></span>";
                $str_content .= "<span style='margin-left:10px;'> ".ODSSHIPNUMBERTXT ."<font style='color:#ff0000;' >". $arr_item['tracking_num']."</font></span></div>";
           }
           $item_cheque_status = $arr_item['cheque_status'] ? $this->constants['ODCHEQUEKEEPEDTXT'] : $this->constants['ODCHEQUEUNKEEPTXT'];
           $check_point = $this->getChequePoint(Order::getDefaultChequeRuleName($arr_item['cheque_rule']));
           $price_total = $this->constants['MONEYTAG'].number_format($arr_item['sale_price']*$arr_item['quantity']+$arr_item['tax'],2);
           $str_content .= <<< str_content
                    </td>
                    <td align='center' style='border-bottom: dashed 1px  #DBDBDB; font-size:12px;' >{$this->constants['OLPRODUCTSELLERNAME']}</td>
                    <td align='center' style='border-bottom: dashed 1px  #DBDBDB; font-size:12px;' >{$this->constants['MONEYTAG']}{$arr_item['sale_price']}</td>
                    <td align='center' style='border-bottom: dashed 1px  #DBDBDB; font-size:12px;' >{$arr_item['quantity']}</td>
                    <td align='center' style='border-bottom: dashed 1px  #DBDBDB; font-size:12px;' >($item_cheque_status)</td>
                    <td align='center' style='border-bottom: dashed 1px  #DBDBDB; font-size:12px;' >$check_point</td> 
                    <td align='center' style='border-bottom: dashed 1px  #DBDBDB; font-size:12px;' >$price_total</td>
                </tr>
                        
str_content;
    	}
    	$str_content .= "</table>";
    	return $str_content;
    }

    protected function getChequePoint($str_type){
        if (preg_match('/([\d]{1,2}%)/', $str_type, $arr_search))
            return $arr_search[1];
        return '0%';
    }

    protected function getOrderListTableHead(){
       
               
         $str_table = <<< str_table
    	     <table class="zi_table" cellpadding='0' cellspacing='0' border='0' style='width:760px; '>
    	         <tr>
    	             <th style='width:370px;height:36px; border-bottom: dashed 1px  #DBDBDB; font-size:12px;'>{$this->constants['OLORDERITEMTH0']}</th>
    	             <th style='width:100px; border-bottom: dashed 1px  #DBDBDB; font-size:12px;'>{$this->constants['OLPRODUCTSELLERTXT']}</th>
    	             <th style='width:50px; border-bottom: dashed 1px  #DBDBDB; font-size:12px;'>{$this->constants['OLORDERITEMTH1']}</th>
    	             <th style='width:50px; border-bottom: dashed 1px  #DBDBDB; font-size:12px;'>{$this->constants['OLORDERITEMTH2']}</th>
    	             <th style='width:90px; border-bottom: dashed 1px  #DBDBDB; font-size:12px;'>{$this->constants['OLORDERITEMTH3']}</th>
    	             <th style='width:50px; border-bottom: dashed 1px  #DBDBDB; font-size:12px;'>{$this->constants['OLORDERITEMTH4']}</th>
    	             <th style='width:50px; border-bottom: dashed 1px  #DBDBDB; font-size:12px;'>{$this->constants['OLORDERITEMTH5']}</th>
    	         </tr>
    	    
    	     
str_table;
    	  
    	  return  $str_table;
    }

    protected function getMessage(){
        $str_title = constant("ABOUTORDERSTATUS{$this->order['status']}TITLE");
        $str_content = constant("ABOUTORDERSTATUS{$this->order['status']}MESSAGE");
        if (empty($str_title))
            return '';
        $str_result = "<div class='ts_border'><p class='title'>{$str_title}</p>%s</div>";
        if (!empty($str_content))
            $str_content = "<p class='list_1'>{$str_content}</p>";
        return sprintf($str_result,$str_content);
    }

    protected function getOrderNav(){
        /**
         * if status is 6,7,16,17,26 or 27, return
         */
        if ($this->order['status'] < 1 || $this->order['status'] % 10 > 5)
            return '';

        if ($this->order['status'] < 6)
            $arr_nav = $this->aboutOrderStepUser;
        elseif ($this->order['status'] < 16)
            $arr_nav = $this->aboutOrderStepAgent;
        else
            return '';

        $str_result = '<ul>';
        $str_constant = constant( "ABOUTORDERSTATUS{$this->order['status']}TXT" );
        foreach ($arr_nav as $int_key => $str_string){
            $str_result.= "<li class='nvaC_" . ($int_key+1) . "'>" . $str_string;
            if ($str_string == $str_constant)
                $str_result.= "<div class='label_1'><p class='nva_1'>{$str_string}</p></div>";
            $str_result.= '</li>';
        }
        return "<div class='right_nav'>{$str_result}</div>";
    }

    protected function getTitle() {
        return "<p class='style_1'>" . ODSORDERSTATUSTXT . '</p>'
               . '<p>' . OLORDERIDTXT . '<span>' . $this->order['id'] . '</span></p>'
               . '<p>' . OLORDERNUMTXT . '<span>' . $this->order['order_num'] . '</span></p>'
               . '<p>' . OLORDERCREATEDTIMETXT . $this->order['created'] . '</p>';
    }

    protected function beableToAction($int_userid, $arr_order){
    	if( $arr_order['is_returned']) {
    		return false;
    	}
        if( $arr_order['status'] > 0 && $arr_order['status'] < 10 ){
            if( $int_userid != $arr_order['user_id'] )
              return false;
            return true;
        }else{
           if( $int_userid != $arr_order['agent_id'] )
              return false;
           return true;
        }
    }

}
?>